package org.jivesoftware.smack.sasl;

import de.measite.smack.Sasl;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Base64;

public class SASLFacebookConnect extends SASLMechanism
{
  private String sessionKey = "";
  private String sessionSecret = "";
  private String apiKey = "";

  static
  {
    SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLFacebookConnect.class);
    SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
  }

  public SASLFacebookConnect(SASLAuthentication paramSASLAuthentication)
  {
    super(paramSASLAuthentication);
  }

  protected void authenticate()
    throws IOException, XMPPException
  {
    final StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<auth mechanism=\"").append(getName());
    localStringBuilder.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
    localStringBuilder.append("</auth>");
    getSASLAuthentication().send(new Packet()
    {
      public String toXML()
      {
        return localStringBuilder.toString();
      }
    });
  }

  public void authenticate(String paramString1, String paramString2, String paramString3)
    throws IOException, XMPPException
  {
    if ((paramString1 == null) || (paramString3 == null))
      throw new IllegalStateException("Invalid parameters!");
    String[] arrayOfString1 = paramString1.split("\\|");
    if ((arrayOfString1 == null) || (arrayOfString1.length != 2))
      throw new IllegalStateException("Api key or session key is not present!");
    this.apiKey = arrayOfString1[0];
    this.sessionKey = arrayOfString1[1];
    this.sessionSecret = paramString3;
    this.authenticationId = this.sessionKey;
    this.password = paramString3;
    this.hostname = paramString2;
    String[] arrayOfString2 = { "DIGEST-MD5" };
    HashMap localHashMap = new HashMap();
    this.sc = Sasl.createSaslClient(arrayOfString2, null, "xmpp", paramString2, localHashMap, this);
    authenticate();
  }

  public void authenticate(String paramString1, String paramString2, CallbackHandler paramCallbackHandler)
    throws IOException, XMPPException
  {
    String[] arrayOfString = { "DIGEST-MD5" };
    HashMap localHashMap = new HashMap();
    this.sc = Sasl.createSaslClient(arrayOfString, null, "xmpp", paramString2, localHashMap, paramCallbackHandler);
    authenticate();
  }

  protected String getName()
  {
    return "X-FACEBOOK-PLATFORM";
  }

  public void challengeReceived(String paramString)
    throws IOException
  {
    final StringBuilder localStringBuilder = new StringBuilder();
    byte[] arrayOfByte = null;
    if (paramString != null)
    {
      str1 = new String(Base64.decode(paramString));
      Map localMap = getQueryMap(str1);
      String str2 = "1.0";
      String str3 = (String)localMap.get("nonce");
      String str4 = (String)localMap.get("method");
      Long localLong = Long.valueOf(new GregorianCalendar().getTimeInMillis() / 1000L);
      String str5 = "api_key=" + this.apiKey + "call_id=" + localLong + "method=" + str4 + "nonce=" + str3 + "session_key=" + this.sessionKey + "v=" + str2 + this.sessionSecret;
      try
      {
        str5 = MD5(str5);
      }
      catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
      {
        throw new IllegalStateException(localNoSuchAlgorithmException);
      }
      String str6 = "api_key=" + this.apiKey + "&" + "call_id=" + localLong + "&" + "method=" + str4 + "&" + "nonce=" + str3 + "&" + "session_key=" + this.sessionKey + "&" + "v=" + str2 + "&" + "sig=" + str5;
      arrayOfByte = str6.getBytes();
    }
    String str1 = "";
    if (arrayOfByte != null)
      str1 = Base64.encodeBytes(arrayOfByte, 8);
    localStringBuilder.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
    localStringBuilder.append(str1);
    localStringBuilder.append("</response>");
    getSASLAuthentication().send(new Packet()
    {
      public String toXML()
      {
        return localStringBuilder.toString();
      }
    });
  }

  private Map<String, String> getQueryMap(String paramString)
  {
    String[] arrayOfString1 = paramString.split("&");
    HashMap localHashMap = new HashMap();
    for (String str1 : arrayOfString1)
    {
      String str2 = str1.split("=")[0];
      String str3 = str1.split("=")[1];
      localHashMap.put(str2, str3);
    }
    return localHashMap;
  }

  private String convertToHex(byte[] paramArrayOfByte)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < paramArrayOfByte.length; i++)
    {
      int j = paramArrayOfByte[i] >>> 4 & 0xF;
      int k = 0;
      do
      {
        if ((j >= 0) && (j <= 9))
          localStringBuffer.append((char)(48 + j));
        else
          localStringBuffer.append((char)(97 + (j - 10)));
        j = paramArrayOfByte[i] & 0xF;
      }
      while (k++ < 1);
    }
    return localStringBuffer.toString();
  }

  public String MD5(String paramString)
    throws NoSuchAlgorithmException, UnsupportedEncodingException
  {
    MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
    byte[] arrayOfByte = new byte[32];
    localMessageDigest.update(paramString.getBytes("iso-8859-1"), 0, paramString.length());
    arrayOfByte = localMessageDigest.digest();
    return convertToHex(arrayOfByte);
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.sasl.SASLFacebookConnect
 * JD-Core Version:    0.6.1
 */